
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/

#pragma once

//storage
#include <QLinkedList>

#include "CVector3f.h"

using std::list;

class Movable;
class Geometry;
class GeoSphere;
class GeoBox;
class GeoCylinder;
class GeoCone;
class GeoTorus;
class GeoVertex;
class GeoMesh;

class GUIILight;
class DirectionalLight;
class PointLight;
class SpotLight;

class CameraEye;
class CameraAt;
class CameraUp;

#include <string>
using std::string;

#include <fstream>
using std::ofstream;
using std::ifstream;

//class CMaterial;

//This class store, draw and handle all the primitives of the scene.
class Scene
{
private:
	//instance object
	static Scene* _instance;

public:
	//storage for the primitives
	QLinkedList<Geometry*> _primitives;

	//storage for the lights
	QLinkedList<GUIILight*> _lights;

	//storage for materials
	//vector<CMaterial*> _materials;

private:
	//the current selected object in the scene
	Movable* _selectedObject;

	//the first selected object in the scene
	Movable* _firstSelectedObject;

	//camera position
	CVector3f _eye;

	//camera at
	CVector3f _at;

	//camera up
	CVector3f _up;

	unsigned int _primitiveCount;

	//number of light used
	int _lightID;

	//camera
	CameraEye* _cameraEye;
	CameraAt* _cameraAt;
	CameraUp* _cameraUp;

	//global ambient light
	float _globalAmbientLight[4];

private:
	//constructor
	Scene();

	//destructor
	~Scene();

	//save a mesh
	bool saveMesh(ofstream& ofs, GeoMesh* m);

	//load a mesh
	bool loadMesh(ifstream& ifs, GeoMesh* m);

public:
	//return the singleton
	static Scene& getInstance();

	//destroy the singleton
	void close();

	//Create sphere and add it to the scene. Return a pointer to the sphere object.
	GeoSphere* createSphere();

	//Create box and add it to the scene. Return a pointer to the sphere object.
	GeoBox* createBox();

	//Create a cylinder and add it to the scene. Return a pointer to the sphere object.
	GeoCylinder* createCylinder();

	//Create a cone and add it to the scene. Return a pointer to the sphere object.
	GeoCone* createCone();

	//Create a torus and add it to the scene. Return a pointer to the sphere object.
	GeoTorus* createTorus();

	//Create a vertex and add it to the scene. Return a pointer to the vertex object.
	GeoVertex* createVertex();

	//Create a mesh and add it to the scene. Return a pointer to the vertex object.
	GeoMesh* createMesh();

	//Create a directionnal light and return a pointer to it.
	DirectionalLight* createDirectionalLight();

	//Create a point light and return a pointer to it.
	PointLight* createPointLight();

	//Create a spot lighht and return a pointer to it.
	SpotLight* createSpotLight();

	//Find the primitives corresponding to the point. Return 0 if no primitives is found.
	Movable* getMovable(const CVector3f& v);
	
	//Draw the entire scene
	void drawScene();

	//draw scene as render
	void drawSceneAsRender();

	//set the selected object
	void setSelectedObject(Movable* g);

	//unselect the current selected object
	void unselectObject();

	//set the camera position
	void setCameraEye(const CVector3f& e);

	//return the camera position
	CVector3f getCameraEye()const;

	//return the selected object
	Movable* getSelectedObject()const;

	//create a new unique name for a primitive
	void setPrimitiveNewName(string& n);

	//delete a primitive
	void deletePrimitive(Geometry* g);

	//delete a light
	void deleteLight(GUIILight* l);

	//delete the selected object. Return true if the object has been deleted.
	bool deleteSelectedObject();

	//get the current light ID max
	int getLightID()const;

	//set tje light id
	void setLightID(int lightID);

	//get the camera object
	CameraEye* getCamera()const;

	//set the global ambient light
	void setGlobalAmbientLight(float r, float g, float b, float a);

	//get the global ambient light of the scene
	float* getGlobalAmbientLight();

	//save the scene into a file
	bool saveScene(const string& filename);

	//load the scene from a file
	bool loadScene(const string& filename);

	//delete all the primitives of the scene
	void deleteAllPrimitives();

	//delete all the lights of the scene
	void deleteAllLights();

	//set the RT camera
	void setRTCamera(const CVector3f& eyeElement, const CVector3f& atElement, const CVector3f& upElement);

	//unselect all the primitives
	void unselectAll();

	//get the first selected object
	Movable* getFirstSelectedObject();

	//set the first selected object
	void setFirstSelectedObject(Movable* firstSelectedObject);
};

#define SCENE Scene::getInstance()